<template>
  <el-dialog
    top="10vh"
    width="500px"
    title="分配角色"
    :visible.sync="dialogValue"
    :close-on-click-modal="false"
    append-to-body
  >
    <SubmitForm
      :rule-form="ruleForm"
      :rules="rules"
      :form-list="formList"
      @change="handleFormChange"
      @save="onSave"
      @close="dialogValue = false"
    />

  </el-dialog>
</template>

<script>
import { http } from '@/api'
export default {
  name: 'FormAdd',
  data() {
    return {
      apiKey: 'user',
      dialogValue: false,
      ruleForm: {
        userId: undefined,
        roleIds: []
      },
      defaultForm: {
        userId: undefined,
        roleIds: []
      },
      rules: {
      },
      formList: [
        {
          prop: 'roleIds',
          label: '角色',
          name: 'select',
          multiple: true,
          options: [],
          colSpan: 24
        },
      ]
    }
  },
  methods: {
    handleFormChange({ key, val }) {

    },
    open(row) {
      console.log('row',row)
      this.dialogValue = true
      http.role.list().then(res => {
        this.formList[0].options = res.data.map(item => {
          return {
            value: item.id,
            label: item.name
          }
        })
      })
      if (row) {
        http.user.getUserRoles(row.id).then(res => {
          this.ruleForm.roleIds = res.data
          this.ruleForm.userId = row.id
        })
      } else {
        this.ruleForm = {
          ...this.defaultForm
        }
      }

    },
    onSave() {
      http[this.apiKey].updateUserRoles({ ...this.ruleForm }).then(res => {
        this.$message.success('操作成功')
        this.dialogValue = false
        this.$emit('get-page')
      })
    }
  }
}
</script>

<style scoped lang="scss">
</style>
